﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Marbles.Core.Connectors;

namespace Marbles.Core.BBS
{
    /// <summary>
    /// Forum Database Object
    /// </summary>
    public class Forum
    {
        public string Siteid { get { return _siteid; } }
        public string Category { get { return _category; } set { _category = value; } }
        public string CategoryTitle { get { return _categorytitle; } }
        public string ForumName { get { return _forumname; } }
        public string Title { get { return _title; } set { _title = value; } }
        public string Description { get { return _description; } set { _description = value; } }
        public string Moderator { get { return _moderator; } set { _moderator = value; } }
        public bool IsReadonly { get { return _isreadonly; } set { _isreadonly = value; } }
        public bool IsPublic { get { return _ispublic; } set { _ispublic = value; } }
        public bool IsModerated { get { return _ismoderated; } set { _ismoderated = value; } }
        public Int64 LastPostId { get { return _lastpostid; } }
        public string LastPostTitle { get { return _lastposttitle; } }
        public string LastPostAuthor { get { return _lastpostby; } }
        public DateTime LastPostDate { get { return _lastpostdate; } }
        public string LastPostDateString { get { return (LastPostDate.Year <= 1980) ? "" : LastPostDate.ToString("ddd") + " " + LastPostDate.ToShortDateString() + " " + LastPostDate.ToShortTimeString(); } }
        public Int64 PostCount { get { return _postcount; } }
        public Int64 ThreadCount { get { return _threadcount; } }

        private Int64 _threadcount = 0;
        private Int64 _postcount = 0;
        private string _category = string.Empty;
        private string _categorytitle = string.Empty;
        private string _siteid = string.Empty;
        private string _forumname = string.Empty;
        private string _title = string.Empty;
        private string _description = string.Empty;
        private string _moderator = string.Empty;
        private bool _isreadonly = false;
        private bool _ispublic = false;
        private bool _ismoderated = false;
        private Int64 _lastpostid = 0;
        private DateTime _lastpostdate = DateTime.MinValue;
        private string _lastposttitle = string.Empty;
        private string _lastpostby = string.Empty;

        public Forum(string siteid, string forumname)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT a.*, b.title, b.postby, b.postdate, c.title AS categorytitle FROM forumheader a LEFT JOIN forumitems b ON a.siteid = b.siteid AND a.forumname = b.forumname AND a.lastpostid = b.postid INNER JOIN forumcategory c ON a.siteid = c.siteid AND a.category = c.category WHERE a.siteid = @siteid AND a.forumname = @forumname");
            cmd.AddParameter("@siteid", siteid);
            cmd.AddParameter("@forumname", forumname);
            _siteid = siteid;
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0)
            {
                _threadcount = Convert.ToInt64(dt.Rows[0]["threadcount"]);
                _postcount = Convert.ToInt64(dt.Rows[0]["postcount"]);
                _category = dt.Rows[0]["category"].ToString();
                _categorytitle = dt.Rows[0]["categorytitle"].ToString();
                _siteid = dt.Rows[0]["siteid"].ToString();
                _forumname = dt.Rows[0]["forumname"].ToString().ToLower();
                _title = dt.Rows[0]["title"].ToString();
                _description = dt.Rows[0]["description"].ToString();
                _moderator = dt.Rows[0]["moderator"].ToString().ToLower();
                _isreadonly = Convert.ToBoolean(dt.Rows[0]["isreadonly"]);
                _ispublic = Convert.ToBoolean(dt.Rows[0]["ispublic"]);
                _ismoderated = Convert.ToBoolean(dt.Rows[0]["ismoderated"]);
                _lastpostid = Convert.ToInt64(dt.Rows[0]["lastpostid"]);
                if (_lastpostid != 0)
                {
                    if (dt.Rows[0]["postdate"] != DBNull.Value)
                    {
                        _lastposttitle = dt.Rows[0]["title"].ToString();
                        _lastpostby = dt.Rows[0]["postby"].ToString();
                        _lastpostdate = Convert.ToDateTime(dt.Rows[0]["postdate"]);
                    }
                }
            }
        }

        internal Forum(DataRow row)
        {
            _threadcount = Convert.ToInt64(row["threadcount"]);
            _postcount = Convert.ToInt64(row["postcount"]);
            _category = row["category"].ToString();
            _categorytitle = row["categorytitle"].ToString();
            _siteid = row["siteid"].ToString();
            _forumname = row["forumname"].ToString().ToLower();
            _title = row["title"].ToString();
            _description = row["description"].ToString();
            _moderator = row["moderator"].ToString().ToLower();
            _isreadonly = Convert.ToBoolean(row["isreadonly"]);
            _ispublic = Convert.ToBoolean(row["ispublic"]);
            _ismoderated = Convert.ToBoolean(row["ismoderated"]);
            _lastpostid = Convert.ToInt64(row["lastpostid"]);
            if (_lastpostid != 0)
            {
                if (row["postdate"] != DBNull.Value)
                {
                    _lastposttitle = row["title"].ToString();
                    _lastpostby = row["postby"].ToString();
                    _lastpostdate = Convert.ToDateTime(row["postdate"]);
                }
            }
        }

        /// <summary>
        /// Clear the current Forum Definition
        /// </summary>
        public void Clear()
        {
            _threadcount = 0;
            _postcount = 0;
            _category = string.Empty;
            _categorytitle = string.Empty;
            _siteid = string.Empty;
            _forumname = string.Empty;
            _title = string.Empty;
            _description = string.Empty;
            _moderator = string.Empty;
            _isreadonly = false;
            _ispublic = false;
            _ismoderated = false;
            _lastpostid = 0;
            _lastpostdate = DateTime.MinValue;
            _lastposttitle = string.Empty;
            _lastpostby = string.Empty;
        }

        /// <summary>
        /// Creates a New Forum Definition
        /// </summary>
        /// <param name="siteid">Associated SiteId</param>
        /// <param name="forumname">Forum Name</param>
        /// <returns>new Forum Definition</returns>
        public static Forum CreateNew(string siteid, string forumname)
        {
            return new Forum(siteid, forumname);
        }


        /// <summary>
        /// Saves the current Forum Definition to the database
        /// </summary>
        public void Update()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE forumheader SET category = @category, title = @title, description = @description, moderator = @moderator, isreadonly = @isreadonly, ispublic = @ispublic, ismoderated = @ismoderated WHERE siteid = @siteid AND forumname = @forumname");
            cmd.AddParameter("@category", _category);
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@forumname", _forumname);
            cmd.AddParameter("@title", _title);
            cmd.AddParameter("@description", _description);
            cmd.AddParameter("@moderator", _moderator);
            cmd.AddParameter("@isreadonly", _isreadonly);
            cmd.AddParameter("@ispublic", _ispublic);
            cmd.AddParameter("@ismoderated", _ismoderated);
            dbc.ExecuteCommand(cmd);
        }

        /// <summary>
        /// Update the last PostId value flag
        /// </summary>
        /// <param name="PostId">PostId of Forum Post</param>
        public void UpdateLastPostid(Int64 PostId)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE forumheader SET lastpostid = @lastpostid WHERE siteid = @siteid AND forumname = @forumname");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@forumname", _forumname);
            cmd.AddParameter("@lastpostid", _lastpostid);
            dbc.ExecuteCommand(cmd);
        }


        /// <summary>
        /// Delete the current forum and posts from the database.
        /// </summary>
        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM forumheader WHERE siteid = @siteid AND forumname = @forumname;DELETE FROM forumitems WHERE siteid = @siteid AND forumname = @forumname;");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@forumname", _forumname);
            dbc.ExecuteCommand(cmd);
        }


        /// <summary>
        /// Writes the current forum definition as a new record.
        /// </summary>
        public void Insert()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("INSERT INTO forumheader (category, siteid, forumname, title, description, moderator, isreadonly, ispublic, ismoderated, lastpostid) VALUES (@category, @siteid, @forumname, @title, @description, @moderator, @isreadonly, @ispublic, @ismoderated, @lastpostid)");
            cmd.AddParameter("@category", _category);
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@forumname", _forumname);
            cmd.AddParameter("@title", _title);
            cmd.AddParameter("@description", _description);
            cmd.AddParameter("@moderator", _moderator);
            cmd.AddParameter("@isreadonly", _isreadonly);
            cmd.AddParameter("@ispublic", _ispublic);
            cmd.AddParameter("@ismoderated", _ismoderated);
            cmd.AddParameter("@lastpostid", _lastpostid);
            dbc.ExecuteCommand(cmd);
        }

        /// <summary>
        /// Get a collection of Forum Posts for associated forum.
        /// </summary>
        /// <returns>ForumPostCollection</returns>
        public ForumPostCollection GetPosts()
        {
            ForumPostCollection items = new ForumPostCollection(this, true);

            return items;
        }
    }
}
